package com.example.sample;

import com.ohos.volley.Response;
import com.ohos.volley.VolleyError;
import com.ohos.volley.ext.LocalHiLog;
import com.ohos.volley.toolbox.ImageLoader;
import com.ohos.volley.toolbox.ImageRequest;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.Image;
import ohos.media.image.PixelMap;

public class ImageLoaderAbility extends Ability implements Component.ClickedListener {
    private Image imageResult;
    private Button buttonImageLoader;
    private Button buttonImageRequest;
    private String str = "https://www.pngfind.com/pngs/m/38-385426_lion_png_lion_background-for-photoshop-transparent-png.png";
    private String str1 = "https://steemitimages.com/DQmP3wYMdtKpwNLYuzXxwQgFqYkSozsUZa3JtZ6rkFXFbCb/4-ways-cheer-up-depressed-cat.jpg";

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_image_loader);

        buttonImageLoader = (Button) findComponentById(ResourceTable.Id_button_image_loader);
        buttonImageRequest = (Button) findComponentById(ResourceTable.Id_button_image_Request);
        imageResult = (Image) findComponentById(ResourceTable.Id_image_result);
        buttonImageLoader.setClickedListener(this);
        buttonImageRequest.setClickedListener(this);

    }

    @Override
    public void onClick(Component component) {
        imageResult.setPixelMap(null);
        switch (component.getId()) {
            case ResourceTable.Id_button_image_loader:
                imaLoaderTest();
                break;
            case ResourceTable.Id_button_image_Request:
                imaRequestTest();
                break;
        }
    }

    /**
     * ImageLoader
     */
    private void imaLoaderTest() {

        VolleyHelper.getInstance(getContext()).getImageLoader().get(str, new ImageLoader.ImageListener() {
            @Override
            public void onResponse(ImageLoader.ImageContainer response, boolean isImmediate) {
                imageResult.setPixelMap(response.getBitmap());
            }

            @Override
            public void onErrorResponse(VolleyError error) {
                LocalHiLog.error("ImageLoader -->  ", error.getMessage());

            }
        }, 480, 640);

    }


    /**
     * ImageRequest
     */
    private void imaRequestTest() {

        ImageRequest imageRequest = new ImageRequest(str1, new Response.Listener<PixelMap>() {
            @Override
            public void onResponse(PixelMap response) {
                imageResult.setPixelMap(response);
            }
        }, 0, 0, Image.ScaleMode.ZOOM_END, null, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                LocalHiLog.error("ImageRequest -->  ", error.getMessage());
            }
        });

        VolleyHelper.getInstance(getContext()).getRequestQueue().add(imageRequest);

    }

}

